package com.atguigu.flink.chapter11;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;


public class Flink12_Hive {
       public static void main(String[] args) {
               Configuration configuration = new Configuration();
               //web  UI端口
               configuration.setInteger("rest.prot",10000);
               StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
               env.setParallelism(2);

           StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

           //第二种数据
           DataStreamSource<WaterSensor> waterSensorStream =
                   env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                           new WaterSensor("sensor_1", 2000L, 20),
                           new WaterSensor("sensor_2", 3000L, 30),
                           new WaterSensor("sensor_1", 4000L, 40),
                           new WaterSensor("sensor_1", 5000L, 50),
                           new WaterSensor("sensor_2", 6000L, 60));

           Table table = tableEnv.fromDataStream(waterSensorStream);
           tableEnv.createTemporaryView("sensor",table);
           //第二种数据


           //1、先创建 hiveCatalog
           HiveCatalog hive = new HiveCatalog("hive", "gmall", "input/");

           // 2. 注册hiveCatalog
           tableEnv.registerCatalog("hive",hive);
        //   tableEnv.useCatalog("hive"); // 设置默认的catalog
         // tableEnv.useDatabase("gmall");

           // 3. 使用hiveCatalog
           tableEnv.sqlQuery("select * from hive.gmall.stu").execute().print();

           //查询第二种数据
          // tableEnv.sqlQuery("select * from default_catalog.default_database.sensor").execute().print();
           //或者 采用以下方式，同理
           tableEnv.useCatalog("default_catalog");
           tableEnv.sqlQuery("select * from sensor").execute().print();





           }
}
